package com.by.component.vod;

import cn.hutool.core.util.IdUtil;
import com.by.component.vod.dto.VodAppInfoSDTO;
import com.by.component.vod.mapper.VodAppInfoMapper;
import com.by.component.vod.mapper.VodFileMapper;
import com.by.toolkit.platform.ByContext;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import static com.by.toolkit.utils.ExceptionUtil.nullError;

@Slf4j
@DubboService
public class VodFileServiceImpl implements VodFileService {

    @Resource
    private VodSupport vodSupport;

    @Resource
    private VodAppInfoMapper vodAppInfoMapper;

    @Resource
    private VodFileMapper vodFileMapper;

    @Override
    public String getUploadSignature(Integer vodFileType) {
        VodAppInfoSDTO vodAppInfo = vodAppInfoMapper.vodAppInfo(ByContext.getProjectId());
        nullError(vodAppInfo, "暂未绑定点播app");

        String fileCode = IdUtil.fastUUID();
        Long vodAppId = vodAppInfo.getVodAppId();
        VodFile vodFile = VodFile.builder()
                .fileCode(fileCode)
                .fileType(vodFileType)
                .vodAppId(vodAppId)
                .build();
        String mediaProcedureTask = vodAppInfo.getMediaProcedureTask();
        if (StringUtils.isNotBlank(mediaProcedureTask)) {
            vodFile.setProcedureTask(mediaProcedureTask);
        }
        vodFileMapper.insert(vodFile);

        String uploadSignature = vodSupport.getUploadSignature(vodAppId, fileCode, mediaProcedureTask);
        nullError(uploadSignature, "生成点播上传签名异常，请联系管理员！");
        return uploadSignature;
    }
}
